local M = {}

local keys = require("tool.key_tool")

local platform = require("tool.platform")
--- 根据平台选择按键
---@param mac_key any
---@param default_key any
---@return any
local function platform_key(mac_key, default_key)
  return platform.isMac() and mac_key or default_key
end

function M.config()
  vim.cmd("cmap W w")
  vim.cmd("cmap Q q")
  vim.cmd("cmap <C-c> <C-u>") --清除输入的命令

  -- 注释
  local ek = platform_key("<D-_>", "<C-_>")
  local ekFunc = function()
    local mode = vim.api.nvim_get_mode().mode
    vim.api.nvim_input("<Esc><Plug>(comment_toggle_linewise_current)<Down>")
    if mode == "i" then
      vim.api.nvim_input("i")
    end
  end
  keys.map({ "n", "i" }, ek, ekFunc)
  keys.xmap(ek, "<Plug>(comment_toggle_linewise_visual)")


  -- Save File
  local key = platform_key("<D-s>", "<C-s>")
  local saveFunc = function()
    local m = vim.api.nvim_get_mode().mode
    vim.api.nvim_input("<Esc>:w<cr>")
    if m == "i" then
      vim.api.nvim_input("a")
    end
  end
  keys.map({ "n", "i" }, key, saveFunc)

  -- Delete a line
  keys.map({ "n", "i", "v" }, "<C-y>", vim.api.nvim_del_current_line)

  -- Copy line
  local copyFunc = function()
    local mode = vim.api.nvim_get_mode().mode
    local l = vim.fn.line(".")
    local c = vim.fn.col(".")
    vim.api.nvim_input("<ESC>yyp")
    vim.schedule(function()
      vim.fn.cursor(l + 1, c, 0)
      if mode == "i" then
        vim.api.nvim_input("i")
      end
    end)
  end
  local copyKey = platform_key("<D-d>", "<C-d>")
  keys.map({ "n", "i", "v" }, copyKey, copyFunc)

  -- Ctrl-z
  local revokeFunc = function()
    local mode = vim.api.nvim_get_mode().mode
    vim.api.nvim_input("<Esc>u")
    if mode == "i" then
      vim.api.nvim_input("i")
    end
  end
  keys.map({ "n", "i", "v" }, "<C-z>", revokeFunc)
  -- map({ "n", "i", "v" }, "<S-z>", "<Esc><C-r>")

  -- buffer window
  keys.map({ "n", }, "<Tab>", ":bNext<CR>")
  keys.map({ "n", }, "<S-Tab>", ":bprevious<CR>")

  local treeFunc = function() vim.cmd("NvimTreeToggle") end
  local treeKey = platform_key("<D-t>", "<C-t>")
  keys.map({ "n", "i", "v" }, treeKey, treeFunc)

  -- Formater
  local fmtKey = "<C-A-l>"
  keys.map({ "n", "i" }, fmtKey, vim.lsp.buf.format)

  -- New line
  -- nmap("<C-A-CR>", ":o")
  -- imap("<C-A-CR>", "<ESC>o")

  -- Lvim reload
  keys.map({ "n", "i" }, "<F12>", "<Esc>:LvimReload<CR>")

  -- close buffer window
  keys.map({ "n", "i" }, "<C-q>", "<Esc>:q<CR>")
  keys.map({ "n", "i" }, "<C-w>", "<Esc>:BufferKill<CR>")

  -- window navigation
  keys.map({ "n", "i" }, "<C-Left>", "<Esc><C-w>h")
  keys.map({ "n", "i" }, "<C-Right>", "<Esc><C-w>l")
  keys.map({ "n", "i" }, "<C-Up>", "<Esc><C-w>k")
  keys.map({ "n", "i" }, "<C-Down>", "<Esc><C-w>j")

  -- window resize
  local function resize_window(direction, amount)
    local cmd_map = {
      left = "vertical resize -" .. amount,
      right = "vertical resize +" .. amount,
      up = "resize +" .. amount,
      down = "resize -" .. amount,
    }
    vim.cmd(cmd_map[direction])
  end
  keys.map({ "n", "i", "v" }, "<S-Left>", function() resize_window("left", 2) end)
  keys.map({ "n", "i", "v" }, "<S-Right>", function() resize_window("right", 2) end)
  keys.map({ "n", "i", "v" }, "<S-Up>", function() resize_window("up", 2) end)
  keys.map({ "n", "i", "v" }, "<S-Down>", function() resize_window("down", 2) end)
end

return M
